package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.exception.BusinessException;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.List;

@Service
//事务注解
@Transactional
public class EmployeeServiceImpl implements IEmployeeService {

    private EmployeeMapper employeeMapper;

    @Autowired
    public void setEmployeeMapper(EmployeeMapper employeeMapper) {
        this.employeeMapper = employeeMapper;
    }

    @Override
    public void delete(Long id) {

        employeeMapper.deleteByPrimaryKey(id);
        employeeMapper.deleteRelation(id);
    }

    @Transactional
    @Override
    public void insert(Employee employee, Long[] ids) {
        employeeMapper.insert(employee);
        //维护中间表的关系
        if (ids != null) {
            for (Long roleId : ids) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }
    }

    @Override
    public Employee selectOne(Long id) {

        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {

        return employeeMapper.selectAll();
    }
    @Transactional
    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        //删除关系
        employeeMapper.deleteRelation(employee.getId());
        //插入新的关系
        if (ids != null) {
            for (Long roleId : ids) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Employee> employees = employeeMapper.selectForList(qo);
        return new PageInfo(employees);
    }

    @Override
    public Employee empLogin(String username, String password) throws RuntimeException {
        if (StringUtils.isEmpty(username)) {
            throw new BusinessException("账号不能为空");
        }
        if (StringUtils.isEmpty(password)) {
            throw new BusinessException("密码不能为空");
        }
        Employee emp = employeeMapper.selectByUsername(username);
        if (emp == null || !emp.getPassword().equals(password)) {
            throw new BusinessException("账号密码不存在");
        }
        return emp;
    }

    @Override
    public void exportXls(ServletOutputStream outputStream) throws IOException {
        //创建excel文件
        Workbook wb = new HSSFWorkbook();
        //创建sheet
        Sheet sheet = wb.createSheet("员工名单");
        //标题行
        Row row = sheet.createRow(0);
        //定义第一行的头信息
        row.createCell(0).setCellValue("员工姓名");
        row.createCell(1).setCellValue("员工编号");
        row.createCell(2).setCellValue("员工邮箱");
        List<Employee> employees = employeeMapper.selectAll();
        for (int i = 0; i <employees.size() ; i++) {
            Employee emp = employees.get(i);
            row = sheet.createRow(i+1);
            row.createCell(0).setCellValue(emp.getName());
            row.createCell(1).setCellValue(emp.getId());
            row.createCell(2).setCellValue(emp.getEmail());
        }
        //讲文件输出到浏览器中
        wb.write(outputStream);
    }

    @Override
    public void importXls(MultipartFile file) throws IOException {
        Workbook wb = new HSSFWorkbook(file.getInputStream());
        Sheet sheet = wb.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i <=lastRowNum; i++) {
            Row row = sheet.getRow(i);
            Employee employee = new Employee();
            employee.setName(row.getCell(0).getStringCellValue());
            Double numericCellValue = row.getCell(1).getNumericCellValue();
            employee.setAge(numericCellValue.intValue());
            employee.setEmail(row.getCell(2).getStringCellValue());
            employee.setAdmin(false);
            employee.setPassword("666666");
            employeeMapper.insert(employee);
        }

    }
}
